import {
  Controller,
  Post,
  Body,
  Get,
  Param,
  Patch,
  UseGuards,
} from '@nestjs/common';
import { UserService } from './user.service';
import { User as UserModel} from '@prisma/client';
import { CODE, type TRes, type TResponse,  } from '../common/base';
import { returnFail, returnSuccess } from '../common/response'
import { AuthGuard } from '@nestjs/passport';

@Controller('/api/user')
@UseGuards(AuthGuard('jwt'))
export class UserController {
  constructor(
    private readonly userService: UserService,
  ) {}


  @Get('/:userId')
  async queryUserById (@Param('userId') userId: string): Promise<TResponse<UserModel>> {

    if (!userId) {
      return returnFail(CODE.PARAMERROR, 'userId 不能为空')
    }
    const user: TRes<UserModel>  = await this.userService.getUser({ userId: userId })
    if (user.sql) {
      return returnFail(CODE.SQLERROR, user.msg)
    }
    return returnSuccess(user.msg, user.data)
  }

  @Post('/userList')
  async getUsers(@Body('userIds') userIds: string) {
    if (!userIds) return returnFail(201, 'userIds 不能为空')
    const userRes: TRes<UserModel[]> = await this.userService.getUsers(userIds)
    if (userRes.sql) {
      return returnFail(CODE.SQLERROR, userRes.msg)
    }
    return returnSuccess(userRes.msg, userRes.data)
  }

  @Patch('/username')
  async updateUsername(@Body() req: Pick<UserModel, 'userId' | 'username'>) {
    if (!req.userId || !req.username) {
      return returnFail(CODE.PARAMERROR, 'userId 和 username 不能为空')
    }
    const userRes = await this.userService.updateUername(req)
    if (userRes.sql) {
      return returnFail(CODE.SQLERROR, userRes.msg)
    }
    return returnSuccess(userRes.msg, userRes.data)
  }
}
